<template>
  <div class="app-container">
    <el-form ref="vip" :model="vip" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="Vip名称" prop="name" >
            <el-input v-if="vip.id == null" v-model="vip.name" placeholder="请输入Vip名称"/>
            <el-input v-if="vip.id != null" :disabled="true" v-model="vip.name" placeholder="请输入Vip名称"/>
          </el-form-item>
          <el-form-item label="Vip到期时间" prop="expirationTime">
            <div class="block">
              <el-date-picker
                v-model="vip.expirationTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="Vip到期时间"/>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="resetForm">取消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
</template>

<script>

import vip from '../../api/vip'

export default {
  data() {
    return {
      ruleForm: {
        name: '',
        expirationTime: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入Vip名字', trigger: 'blur' }
        ],
        expirationTime: [
          { required: true, message: '请输入到期时间', trigger: 'blur' }
        ]
      },
      vip: {}
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.findById(id)
      console.log(id)
    }
  },
  methods: {
    increaseVIP() {
      vip.increase(this.vip).then(res => {
        this.$message.success(res.message)
        this.$router.push({ path: '/vip/list' })
      })
    },
    reviseVIP() {
      vip.revise(this.vip).then(res => {
        this.$message.success(res.message)
        this.$router.push({ path: '/vip/list' })
      })
    },
    submitForm() {
      this.$refs['vip'].validate((valid) => {
        if (valid) {
          if (this.vip.id != null) {
            this.reviseVIP()
          } else {
            this.increaseVIP()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$router.push({ path: '/vip/list' })
    },
    findById(id) {
      vip.searchById(id).then(res => {
        this.vip = res.data
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
